package cn.dhn_app.horseman_app.service.impl;

import cn.dhn_app.horseman_app.mapper.tokenMapper;
import cn.dhn_app.horseman_app.entity.*;
import cn.dhn_app.horseman_app.service.HorsemanLoginService;
import cn.dhn_app.util.tokenTool;
import cn.itcast.mapper.horsemanMapper;
import cn.itcast.pojo.horseman;
import cn.itcast.pojo.horsemanExample;
import cn.itcast.util.ResponseEntity;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author lyy
 * @Description TODO
 * @PACKAGE_NAME: cn.dhn_app.horseman_app.service.impl
 * @ClassName LoginServiceImpl
 * @Date 2020/8/12 17:56
 * @DAY_NAME_FULL: 星期三
 * @Version 1.0
 **/
@Service
public class HorsemanLoginServiceImpl implements HorsemanLoginService {


    @Resource
    private tokenMapper tokenMapper;

    @Resource
    private horsemanMapper horsemanMapper;

    @Resource
    private tokenTool tokenTool;

    /**
     * 功能描述: <br>
     * 〈骑手登录〉
     * @Author: lyy
     * @Date: 2020/8/12 17:59
     * @Param: [phone, pwd,type]
     * @Return: cn.itcast.util.ResponseEntity
     */
    @Override
    public ResponseEntity horsemanLogin(String number, String pwd,Long type) {

        if(number.isEmpty()) return new ResponseEntity(400,"账号参数不能为空！！！");
        if(pwd.isEmpty()) return new ResponseEntity(400,"密码参数不能为空！！！");
        if(type==null || type==0)return new ResponseEntity(400,"类型参数不能为空！！！");

        //骑手token

        String Token = null;

        JSONObject object = new JSONObject();

        //判断骑手账号是否已经注册
        horsemanExample horsemanExample = new horsemanExample();
        horsemanExample.Criteria criteria = horsemanExample.createCriteria();
        criteria.andHorsemanNumberEqualTo(number);
        criteria.andHorsemanPasswordEqualTo(pwd);
        List<horseman> horsemanList = horsemanMapper.selectByExample(horsemanExample);

        if(horsemanList.isEmpty()) return new ResponseEntity(403,"该账号尚未注册或者密码有误！！！");

        object.put("avatarUrl",horsemanList.get(0).getAvatarUrl());
        object.put("nickName",horsemanList.get(0).getNickName());
        object.put("horsemanPhone",horsemanList.get(0).getHorsemanPhone());

        //账号存在 判断token该用户是否存在token
        tokenExample tokenExample = new tokenExample();
        tokenExample.Criteria tokenCriteria = tokenExample.createCriteria();
        tokenCriteria.andOpenIdEqualTo(horsemanList.get(0).getOpenId());
        tokenCriteria.andTypeEqualTo(type);
        List<token> tokenExist = tokenMapper.selectByExample(tokenExample);

        if(tokenExist.isEmpty()){ //没有token
            //创建token
            token token = new token();
            token.setOpenId(horsemanList.get(0).getOpenId());
            token.setType(Long.valueOf(2));
            token.setCreates(new Date().getTime());
            token.setToken(tokenTool.makeToken());
            int horsemanToken = tokenMapper.insertSelective(token);

            if(horsemanToken == 0) return new ResponseEntity(402,"创建token失败！！！");

            //token创建成功 返回token
            tokenExample tokenExampleBack = new tokenExample();
            tokenExample.Criteria tokenCriteriaBack = tokenExampleBack.createCriteria();
            tokenCriteriaBack.andOpenIdEqualTo(horsemanList.get(0).getOpenId());
            tokenCriteriaBack.andTypeEqualTo(type);
            List<token> tokenExistBack = tokenMapper.selectByExample(tokenExampleBack);

            object.put("openId",tokenExistBack.get(0).getOpenId());
            object.put("token",tokenExistBack.get(0).getToken());
            object.put("type",tokenExistBack.get(0).getType());

            return new ResponseEntity(200,"登录成功",object);
        }else{

            object.put("openId",tokenExist.get(0).getOpenId());
            object.put("token",tokenExist.get(0).getToken());
            object.put("type",tokenExist.get(0).getType());

            //token已存在
            return new ResponseEntity(200,"登录成功",object);
        }
    }

}
